﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class LayAirTerminalForms : System.Windows.Forms.Form
	{
		public LayAirTerminalForms()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public LayAirTerminalForms(TreeView tv, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.hvacDR = new HVACDataReader(doc);
			this.initParameterSetting();
			this.initFamilyTypeCB();
			base.Controls.Add(this.treeView);
			this.initTreeView();
			this.doc = doc;
		}

		private void initParameterSetting()
		{
			UserSetting userSetting = new UserSetting();
			this.hyTbOffSet.Text = userSetting.GetValue("airterminal", "offset");
			this.hyTbFlow.Text = userSetting.GetValue("airterminal", "flow");
			this.hyTbSpeed.Text = userSetting.GetValue("airterminal", "speed");
			this.hyTbDrop.Text = userSetting.GetValue("airterminal", "drop");
			this.hytbAngle.Text = userSetting.GetValue("airterminal", "angle");
			this.dAirVolume = Convert.ToDouble(userSetting.GetValue("AirPortSelForm", "TVolume"));
			this.zoneSizeText.Text = userSetting.GetValue("airterminal", "ZoneRadius");
			this.zoneWidthText.Text = userSetting.GetValue("airterminal", "ZoneWidth");
			this.zoneLengthText.Text = userSetting.GetValue("airterminal", "ZoneLength");
			this.zoneShowRadio.Checked = Convert.ToBoolean(userSetting.GetValue("airterminal", "ZoneTerminalCheck"));
			this.zoneTypeCom.Text = userSetting.GetValue("airterminal", "ZoneType");
			this.enableZoneShow(this.zoneShowRadio.Checked);
			try
			{
				this.cbTag.Checked = Convert.ToBoolean(userSetting.GetValue("airterminal", "tag"));
				this.cbAngle.Checked = Convert.ToBoolean(userSetting.GetValue("airterminal", "angletag"));
			}
			catch (Exception)
			{
			}
		}

		private void initTreeView()
		{
			this.treeView.Location = new System.Drawing.Point(14, 36);
			this.treeView.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.treeView.Width = 200;
			this.treeView.Height = 300;
			this.treeView.NodeMouseClick += this.treeView_NodeMouseClick;
			foreach (object obj in this.treeView.Nodes)
			{
				((TreeNode)obj).Expand();
			}
		}

		private void initFamilyTypeCB()
		{
			this.cmbSystemType.Items.Add("全部");
			foreach (string item in this.hvacDR.GetFamilySort("风口"))
			{
				this.cmbSystemType.Items.Add(item);
			}
			UserSetting userSetting = new UserSetting();
			this.name = userSetting.GetValue("laysetting", "airterminalsel").Split(new char[]
			{
				'|'
			});
			if (this.name.Count<string>() > 0)
			{
				this.cmbSystemType.SelectedItem = this.name[0];
			}
			else
			{
				this.cmbSystemType.SelectedItem = "全部";
			}
			if (this.name.Count<string>() > 1)
			{
				TreeNode treeNode = this.GetUserFamilyNode(this.name[1]);
				if (treeNode != null)
				{
					this.treeView.SelectedNode = treeNode;
				}
				else if (this.treeView.Nodes != null && this.treeView.Nodes.Count > 0)
				{
					treeNode = TreeViewTools.GetFirstNode(this.treeView.Nodes[0]);
					this.treeView.SelectedNode = treeNode;
				}
				this.DisPlayImgAndParameter(treeNode, ref this.treeSelectedNode);
			}
			if (this.name.Count<string>() > 2)
			{
				this.lsbFamilyType.SelectedItem = this.name[2];
			}
		}

		private TreeNode GetUserFamilyNode(string name)
		{
			TreeNode treeNode = null;
			foreach (object obj in this.treeView.Nodes)
			{
				treeNode = TreeViewTools.GetSelectedNode((TreeNode)obj, name);
				if (treeNode != null)
				{
					break;
				}
			}
			return treeNode;
		}

		private void cmbSystemType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cmbSystemType.SelectedItem != null && "" != this.cmbSystemType.SelectedItem.ToString())
			{
				TreeView familyAllInfoTree;
				if ("全部" == this.cmbSystemType.SelectedItem.ToString() || "" == this.cmbSystemType.SelectedItem.ToString())
				{
					familyAllInfoTree = this.hvacDR.GetFamilyAllInfoTree("风口");
				}
				else
				{
					familyAllInfoTree = this.hvacDR.GetFamilyAllInfoTree(this.cmbSystemType.SelectedItem.ToString());
				}
				if (familyAllInfoTree != null)
				{
					base.Controls.Remove(this.treeView);
					this.treeView = familyAllInfoTree;
					base.Controls.Add(this.treeView);
					this.initTreeView();
					this.lsbFamilyType.Items.Clear();
				}
			}
		}

		protected void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			this.DisPlayImgAndParameter(e.Node, ref this.treeSelectedNode);
		}

		protected void DisPlayImgAndParameter(TreeNode treeNode, ref TreeNode tSelectedNode)
		{
			try
			{
				if (treeNode != null && treeNode.Tag is DbFamily)
				{
					Image image = (treeNode.Tag as DbFamily).Image;
					this.picBoxpreview.Image = image;
					this.DisPlayFamilyType(treeNode);
					if (tSelectedNode != null)
					{
						tSelectedNode.BackColor = System.Drawing.Color.Transparent;
						tSelectedNode.ForeColor = System.Drawing.Color.Black;
					}
					tSelectedNode = treeNode;
					treeNode.BackColor = System.Drawing.Color.DodgerBlue;
					treeNode.ForeColor = System.Drawing.Color.White;
					if (this.lsbFamilyType.SelectedItem == null && this.lsbFamilyType.Items.Count > 0)
					{
						this.lsbFamilyType.SelectedItem = this.lsbFamilyType.Items[0];
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void DisPlayFamilyType(TreeNode treeNode)
		{
			if (treeNode != null && treeNode.Tag is DbFamily)
			{
				this.lsbFamilyType.Items.Clear();
				List<string> symbols = (treeNode.Tag as DbFamily).Symbols;
				IComparer<string> comparer = new ListSortByNumber();
				symbols.Sort(comparer);
				foreach (string item in symbols)
				{
					this.lsbFamilyType.Items.Add(item);
				}
			}
		}

		private void lsbFamilyType_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void GetFamilyTypeParameter()
		{
			DbFamily family = this.treeSelectedNode.Tag as DbFamily;
			if (this.lsbFamilyType.SelectedItem != null)
			{
				string typeName = this.lsbFamilyType.SelectedItem.ToString();
				new List<DbParameter>();
				List<DbParameter> symbolParameter = this.hvacDR.GetSymbolParameter(family, typeName);
				this.hyPropertys = new HYPropertys();
				HYProperty[] array = new HYProperty[symbolParameter.Count];
				int num = 0;
				foreach (DbParameter dbParameter in symbolParameter)
				{
					array[num] = new HYProperty();
					if (dbParameter != null && dbParameter.ParamDefinition != null && dbParameter.Value != null && "" != dbParameter.Value.ToString().Trim())
					{
						array[num].Name = dbParameter.ParamDefinition.Name;
						array[num].Description = dbParameter.ParamDefinition.Name;
						array[num].Value = dbParameter.Value + " " + dbParameter.ParamDefinition.Unit;
						array[num].Category = "参数";
						this.hyPropertys.Add(array[num]);
						num++;
					}
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.dLength = Convert.ToDouble(this.zoneLengthText.Text) * 1000.0 / 304.8;
			this.dWidth = Convert.ToDouble(this.zoneWidthText.Text) * 1000.0 / 304.8;
			this.dRadius = Convert.ToDouble(this.zoneSizeText.Text) * 1000.0 / 304.8;
			if (this.hyTbOffSet.Text.Trim() == "" || this.hyTbFlow.Text.Trim() == "" || this.hyTbSpeed.Text.Trim() == "" || this.hyTbDrop.Text.Trim() == "" || this.hytbAngle.Text.Trim() == "")
			{
				MessageBox.Show("所有参数均不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.UserSettingWrite();
			this.initAirTerminalParam();
			try
			{
				if (this.lsbFamilyType.SelectedItem != null && this.treeSelectedNode != null && "" != this.lsbFamilyType.SelectedItem.ToString())
				{
					this.FamilyLoad();
					this.InitLayType();
					if (5 == this.isOnFace)
					{
						if (this.layType != 0 && RevitVersion.Version != "2012")
						{
							MessageBox.Show("基于面设备的批量布置，不支持当前Revit版本", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						}
						else
						{
							SelectPlaneForm selectPlaneForm = new SelectPlaneForm(this.layType, this.hyTbOffSet.Text, this.doc);
							DialogResult dialogResult = selectPlaneForm.ShowDialog();
							if (DialogResult.OK == dialogResult)
							{
								this.planeParameter = selectPlaneForm.planeParam;
							}
							base.DialogResult = dialogResult;
						}
					}
					else
					{
						base.DialogResult = DialogResult.OK;
					}
				}
				else
				{
					MessageBox.Show("请选择族类型", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}

		private void InitLayType()
		{
			if (this.tsbLayPoint.CheckState == CheckState.Checked)
			{
				this.layType = 0;
				return;
			}
			if (this.tsbLayLine.CheckState == CheckState.Checked)
			{
				this.layType = 1;
				return;
			}
			if (this.tsbLayGrid.CheckState == CheckState.Checked)
			{
				this.layType = 2;
				return;
			}
			if (this.tsbLayRect.CheckState == CheckState.Checked)
			{
				this.layType = 3;
				return;
			}
			if (this.tsbLayMid.CheckState == CheckState.Checked)
			{
				this.layType = 4;
			}
		}

		private void FamilyLoad()
		{
			DbFamily family = this.treeSelectedNode.Tag as DbFamily;
			string typeName = this.lsbFamilyType.SelectedItem.ToString();
			DbFamilySymbol familySymbol = this.hvacDR.GetFamilySymbol(family, typeName);
			if (familySymbol != null)
			{
				this.fs = familySymbol.Create(this.doc, ref this.isOnFace);
				return;
			}
			MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void UserSettingWrite()
		{
			try
			{
				UserSetting userSetting = new UserSetting();
				if (this.treeSelectedNode != null && this.lsbFamilyType.SelectedItem != null && this.lsbFamilyType.SelectedItem.ToString() != "")
				{
					string text = string.Concat(new string[]
					{
						this.cmbSystemType.SelectedItem.ToString(),
						"|",
						this.treeSelectedNode.Text,
						"|",
						this.lsbFamilyType.SelectedItem.ToString()
					});
					userSetting.WriteValue("laysetting", "airterminalsel", text);
				}
				userSetting.WriteValue("airterminal", "offset", this.hyTbOffSet.Text);
				userSetting.WriteValue("airterminal", "flow", this.hyTbFlow.Text);
				userSetting.WriteValue("airterminal", "speed", this.hyTbSpeed.Text);
				userSetting.WriteValue("airterminal", "drop", this.hyTbDrop.Text);
				userSetting.WriteValue("airterminal", "angle", this.hytbAngle.Text);
				userSetting.WriteValue("airterminal", "tag", this.cbTag.Checked.ToString());
				userSetting.WriteValue("airterminal", "angletag", this.cbAngle.Checked.ToString());
				userSetting.WriteValue("airterminal", "ZoneRadius", this.zoneSizeText.Text);
				userSetting.WriteValue("airterminal", "ZoneWidth", this.zoneWidthText.Text);
				userSetting.WriteValue("airterminal", "ZoneLength", this.zoneLengthText.Text);
				userSetting.WriteValue("airterminal", "ZoneTerminalCheck", Convert.ToString(this.zoneShowRadio.Checked));
				userSetting.WriteValue("airterminal", "ZoneType", this.zoneTypeCom.Text);
			}
			catch (Exception)
			{
			}
		}

		private void btnMoreParameter_Click(object sender, EventArgs e)
		{
			if (this.lsbFamilyType.SelectedItem != null && this.lsbFamilyType.SelectedItem.ToString() != "")
			{
				this.GetFamilyTypeParameter();
				new ParameterDisplayForm(this.hyPropertys).ShowDialog();
				return;
			}
			MessageBox.Show("请选择族类型", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		public void btnAirPortSel_Click(object sender, EventArgs e)
		{
			AirPortSelForm airPortSelForm = new AirPortSelForm(this.doc, this.dAirVolume);
			DialogResult dialogResult = airPortSelForm.ShowDialog(this);
			if (dialogResult != DialogResult.OK)
			{
				if (dialogResult != DialogResult.Yes)
				{
					return;
				}
				this.strAirPortType = airPortSelForm.strAirPortType;
				base.DialogResult = DialogResult.Yes;
				base.Close();
			}
			else
			{
				if (airPortSelForm.strFamilyType == "" || airPortSelForm.strFamilyName == "")
				{
					return;
				}
				foreach (object obj in this.cmbSystemType.Items)
				{
					if (obj.ToString() == airPortSelForm.strAirPortType)
					{
						this.cmbSystemType.SelectedItem = obj;
					}
				}
				TreeView familyAllInfoTree = this.hvacDR.GetFamilyAllInfoTree(airPortSelForm.strAirPortType);
				if (familyAllInfoTree != null)
				{
					base.Controls.Remove(this.treeView);
					this.treeView = familyAllInfoTree;
					base.Controls.Add(this.treeView);
					this.initTreeView();
					this.lsbFamilyType.Items.Clear();
				}
				if (this.treeView.Nodes.Count != 0)
				{
					TreeNode[] array = this.treeView.Nodes.Find(airPortSelForm.strFamilyName, true);
					this.treeView.SelectedNode = array[0];
					this.treeView.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
					this.treeView.SelectedNode.ForeColor = System.Drawing.Color.White;
					this.treeSelectedNode = this.treeView.SelectedNode;
				}
				if (this.treeView.SelectedNode != null && this.treeView.SelectedNode.Tag is DbFamily)
				{
					this.lsbFamilyType.Items.Clear();
					List<string> symbols = (this.treeView.SelectedNode.Tag as DbFamily).Symbols;
					IComparer<string> comparer = new ListSortByNumber();
					symbols.Sort(comparer);
					foreach (string item in symbols)
					{
						this.lsbFamilyType.Items.Add(item);
					}
				}
				if (this.lsbFamilyType.Items.Count != 0)
				{
					for (int i = 0; i < this.lsbFamilyType.Items.Count; i++)
					{
						if (this.lsbFamilyType.Items[i].ToString() == airPortSelForm.strFamilyType)
						{
							this.lsbFamilyType.SelectedItem = this.lsbFamilyType.Items[i];
							return;
						}
					}
					return;
				}
			}
		}

		private void initAirTerminalParam()
		{
			this.airTerminalPara = new AirTerminalParam();
			this.airTerminalPara.OffSet = Convert.ToDouble(this.hyTbOffSet.Text.Trim());
			this.airTerminalPara.Flow = Convert.ToDouble(this.hyTbFlow.Text.Trim());
			this.airTerminalPara.Speed = Convert.ToDouble(this.hyTbSpeed.Text.Trim());
			this.airTerminalPara.Drop = Convert.ToDouble(this.hyTbDrop.Text.Trim());
			if (this.hytbAngle.Enabled)
			{
				this.airTerminalPara.Angle = Convert.ToDouble(this.hytbAngle.Text.Trim());
			}
			this.airTerminalPara.Tag = this.cbTag.Checked;
		}

		private void tsLayButton_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			foreach (object obj in this.tsLayButton.Items)
			{
				((ToolStripButton)((ToolStripItem)obj)).CheckState = CheckState.Unchecked;
			}
			((ToolStripButton)e.ClickedItem).CheckState = CheckState.Checked;
		}

		private void tsbLayRect_Click(object sender, EventArgs e)
		{
			LayAirTerminalAreaForm layAirTerminalAreaForm = new LayAirTerminalAreaForm(this.us, "风口");
			layAirTerminalAreaForm.ShowDialog();
			this.layAreaParam = layAirTerminalAreaForm.layAreaParameter;
		}

		private void tsbLayLine_Click(object sender, EventArgs e)
		{
			LayAirTerminalLineForm layAirTerminalLineForm = new LayAirTerminalLineForm(this.us, "风口");
			layAirTerminalLineForm.ShowDialog();
			this.layLineParam = layAirTerminalLineForm.layLineParameter;
		}

		private void tsbLayMid_Click(object sender, EventArgs e)
		{
			LayAirTerminalMidForm layAirTerminalMidForm = new LayAirTerminalMidForm(this.us, "风口");
			layAirTerminalMidForm.ShowDialog();
			this.layMidParam = layAirTerminalMidForm.layMidParameter;
		}

		private void tsbLayPoint_Click(object sender, EventArgs e)
		{
		}

		private void cbAngle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbAngle.Checked)
			{
				this.hytbAngle.Enabled = true;
				return;
			}
			this.hytbAngle.Enabled = false;
		}

		private void zoneShowRadio_CheckedChanged(object sender, EventArgs e)
		{
			this.enableZoneShow(this.zoneShowRadio.Checked);
			this.bIsShowTerminalZone = this.zoneShowRadio.Checked;
		}

		private void enableZoneShow(bool bEnable = false)
		{
			this.zoneTypeCom.Enabled = bEnable;
			this.zoneSizeText.Enabled = bEnable;
			this.zoneLengthText.Enabled = bEnable;
			this.zoneWidthText.Enabled = bEnable;
		}

		private void showRectZone(bool bShow = false)
		{
			this.zoneLengthLabel.Visible = bShow;
			this.zoneWidthLabel.Visible = bShow;
			this.zoneLengthText.Visible = bShow;
			this.zoneWidthText.Visible = bShow;
			bool visible = !bShow;
			this.zoneRadiusLabel.Visible = visible;
			this.zoneSizeText.Visible = visible;
		}

		private void zoneTypeCom_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.zoneTypeCom.Text == "矩形")
			{
				this.showRectZone(true);
				this.nTerminalZoneType = 0;
				return;
			}
			this.showRectZone(false);
			this.nTerminalZoneType = 1;
		}

		public FamilySymbol fs;

		public int isOnFace;

		private Document doc;

		public LayAreaParameter layAreaParam;

		public LayLineParameter layLineParam;

		public AirTerminalParam airTerminalPara;

		public LayMidParameter layMidParam;

		public string strAirPortType;

		public double dAirVolume;

		public bool bIsShowTerminalZone;

		public int nTerminalZoneType;

		public double dRadius;

		public double dWidth;

		public double dLength;

		public int layType;

		public ListItemPlane planeParameter;

		private TreeView treeView = new TreeView();

		private TreeNode treeSelectedNode;

		private HVACDataReader hvacDR;

		private HYPropertys hyPropertys;

		private UserSetting us;

		private string[] name;
	}
}
